const app = getApp()
Page({
  data: {
    theme: 'light',
    user: null,
    page: 1,
    size: 20,
    noMore: false,
    list: [],
    loading: {
      list: false,
    }
  },
  onLoad() {
    this.data.onGlobalUserChange = this.onGlobalUserChange.bind(this)
    app.watch('user', this.data.onGlobalUserChange)
    if (app.globalData.user) {
      this.data.user = app.globalData.user
      this.onSearch()
    }
    this.setData({
      theme: wx.getSystemInfoSync().theme || 'light',
      user: app.globalData.user,
    })

    if (wx.onThemeChange) {
      wx.onThemeChange(({theme}) => {
        this.setData({theme})
      })
    }
  },
  onUnload() {
    app.unWatch('user', this.data.onGlobalUserChange)
  },
  onGlobalUserChange(val, pre) {
    this.data.user = val
    this.onSearch()
    this.setData({
      user: val,
    })
  },
  onSearch() {
    if (this.data.user.follow.length) {
      this.listData(true)
    } else {
      this.setData({
        noMore: true,
        list: []
      })
    }
  },
  listData(refresh) {
    if (refresh === true) {
      this.data.noMore = false
      this.data.page = 1
      this.data.list = []
    }
    let { page, size, noMore, list, loading, user} = this.data
    if (noMore) return
    this.setData({
      loading: {
        ...loading,
        list: true
      },
      page: page + 1
    })
    wx.cloud.callFunction({
      name: 'user',
      data: {
        action: 'list',
        data: {
          follow: user.openid,
          page,
          size
        }
      }
    })
    .then(res => {
      let data = res.result.data,
      nm = data.length < size
      data.forEach(i => i.isFollow = true)
      this.setData({
        noMore: nm,
        list: [...list, ...data]
      })
      
    })
    .finally(() => {
      this.setData({
        loading: {
          ...this.data.loading,
          list: false
        }
      })
    })
  },
  onFollowClick(e) {
    const { index } = e.target.dataset
    const { user, list, loading } = this.data
    const other = list[index]
    const follow = user.follow
    const idx = follow.indexOf(other.openid)
    this.setData({
      loading: {
        ...loading,
        [other.openid]: true
      }
    })
    if (idx > -1) {
      wx.cloud.callFunction({
        name: 'user',
        data: {
          action: 'unfollow',
          data: {
            _id: user._id,
            followId: other.openid
          },
        },
      })
      .then((res) => {
        follow.splice(idx, 1)
        other.isFollow = false
        this.setData({
          list: [...list]
        })
        // app.globalData.user = {
        //   ...user,
        //   follow
        // }
      })
      .finally(() => {
        this.setData({
          loading: {
            ...loading,
            [other.openid]: false
          }
        })
      })
    } else {
      wx.cloud.callFunction({
        name: 'user',
        data: {
          action: 'follow',
          data: {
            _id: user._id,
            followId: other.openid
          },
        },
      })
      .then((res) => {
        follow.push(other.openid)
        other.isFollow = true
        this.setData({
          list: [...list]
        })
        // app.globalData.user = {
        //   ...user,
        //   follow
        // }
      })
      .finally(() => {
        this.setData({
          loading: {
            ...loading,
            [other.openid]: false
          }
        })
      })
    }
  },
  onShareAppMessage() {
    return {
      title: '东鸣西应',
      path: '/page/home/index'
    }
  },
})
